package com.shiguo365.market.display.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.maimengmami.waveswiperefreshlayout.WaveSwipeRefreshLayout;
import com.shiguo365.market.R;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.adapter.MarketPriceAdapter;
import com.shiguo365.market.display.base.StatusBarActivity;
import com.shiguo365.market.display.listener.NavigationFinishClickListener;
import com.shiguo365.market.display.util.RefreshUtils;
import com.shiguo365.market.display.view.IMarketPriceView;
import com.shiguo365.market.presenter.contract.IMarketPricePresenter;
import com.shiguo365.market.presenter.implement.MarketPricePresenter;
import com.shiguo365.market.utils.HandlerUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MarketPriceActivity extends StatusBarActivity implements IMarketPriceView, Toolbar.OnMenuItemClickListener,WaveSwipeRefreshLayout.OnRefreshListener{

    private static final String TAG = "MarketPriceActivity";

    @BindView(R.id.market_price_toolbar)
    protected Toolbar toolbar;

    @BindView(R.id.market_price_recycler_view)
    protected RecyclerView recyclerView;

    @BindView(R.id.market_price_refresh_layout)
    protected WaveSwipeRefreshLayout refreshLayout;

    @BindView(R.id.market_price_icon_no_data)
    protected TextView iconNoData;

    private List<JSONObject> mObjects = new ArrayList<JSONObject>();

    private MarketPriceAdapter mAdapter;

    private IMarketPricePresenter mPresenter;

    private String pid = "";

    private int page=0;
    private int size=10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_market_price);

        ButterKnife.bind(this);

        mPresenter = new MarketPricePresenter(this,this);

        toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));
        toolbar.inflateMenu(R.menu.cart);
        toolbar.setOnMenuItemClickListener(this);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new MarketPriceAdapter(this,mPresenter, mObjects);
        recyclerView.setAdapter(mAdapter);



        refreshLayout.setOnRefreshListener(this);

        RefreshUtils.initOnCreate(refreshLayout, this);
        RefreshUtils.refreshOnCreate(refreshLayout, this);

    }

    @Override
    public void onRefresh() {
        mObjects.clear();
        page=0;
        mPresenter.getProductList("",page,size);
        page++;


    }

    @Override
    public void onLoad() {
        mPresenter.getProductList("",page,size);
        page++;
    }

    @Override
    public boolean canLoadMore() {
        return true;
    }

    @Override
    public boolean canRefresh() {
        return true;
    }

    @Override
    public boolean onRefreshProductListResultOk(@NonNull List<JSONObject> result) {
        mObjects.addAll(result);
        mAdapter.notifyDataSetChanged();
        iconNoData.setVisibility(mObjects.size() == 0 ? View.VISIBLE : View.GONE);
        return true;
    }

    @Override
    public void onRefreshProductListFinish() {
        refreshLayout.setRefreshing(false);
        refreshLayout.setLoading(false);
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch(item.getItemId()) {
            case R.id.action_goto_cart:
                Intent intent =new Intent(this,MainActivity.class);
                intent.putExtra("FragmentId",R.id.txt_cart);
                startActivity(intent);
                //this.finish();
        }
        return true;
    }

    private OtherItemAction mMainAction = new OtherItemAction(CartActivity.class);

    private class OtherItemAction implements Runnable {

        private Class gotoClz;

        protected OtherItemAction(Class gotoClz) {
            this.gotoClz = gotoClz;
        }

        @Override
        public void run() {
            Intent intent = new Intent(MarketPriceActivity.this, gotoClz);
            startActivity(intent);

        }

        public void startDelayed() {
            HandlerUtils.postDelayed(this, 100);
        }

    }
}
